<?php

if ($isSortable) { $sortFlag = "ORDER BY $%linkTableName\".\".sort_order ASC"; }

$linkTableInsertSQLnames = $newNamesPostDataSQLString;//substr($newNamesPostDataSQLString,0,-1);
$linkTableInsertSQLvals = $newValuesPostDataSQLString;//substr($newValuesPostDataSQLString,0,-1);

$writePost = "<?php

require_once('database.php');
$%bodyString = \"\";
if (!$%parentId) { $%parentId = $%_GET['id']; }

if ($%_SERVER['REQUEST_METHOD']==\"POST\") {
	$%id = $%_POST['id'];\n
	$%linkedChild = $%_POST['linkedChild'];\n
	if ($%linkedChild == '$tableName') {
		if ($%id) {
$postDataVarsString
			$%SQLedit = \"UPDATE $tableName SET \";	
			$%SQLedit .= \" $editPostDataSQLString \"; 
			$%SQLedit .= \" WHERE id = '$%id' \";
			$%SQLeditResult = $%db->query($%SQLedit);
		}
		else {
$postDataVarsString
			$%SQLnewQuery = \"INSERT INTO $tableName ($linkTableInsertSQLnames) VALUES ($linkTableInsertSQLvals)\";
			$%SQLnewResult = $%db->query($%SQLnewQuery);
			$%newChildId = mysql_insert_id();
			$%SQLnewAssociation = \"INSERT INTO $%linkTableName (parent_id,child_id) VALUES ($%parentId,$%newChildId)\";
			$%SQLnewAssociationResult = $%db->query($%SQLnewAssociation);
		}
	}
}
";
	$writeHead = "
if (!$%linkTableName) {
	$%linkTableName = $%_GET['linkTableName'];
	$%childTableName = $%_GET['childTableName'];
	$%parentId = $%_GET['parentId'];
}

$%SQLlist = \"SELECT * FROM $%linkTableName LEFT JOIN $%childTableName ON $%childTableName\".\".id = $%linkTableName\".\".child_id WHERE $%linkTableName\".\".parent_id = $%parentId $sortFlag\";
//echo $%SQLlist;
//$%SQLlist = \"SELECT * FROM $tableName WHERE $"."parentTable_SQL_where\";
$%SQLlistResult = $%db->query($%SQLlist);
while( $%rowArray = $%db->fetch_array($%SQLlistResult) ) {";
		
	$linkCheck = "".
"	//Being if/else (id)
	if($%id) { ";

	$writeBody = "\n						
		$%bodyString .= \"\%n<div class='listPageButtons'><a class='editBtn positiveBtns' href='$tableName" . "_write.php?id=$%id&sessionDepth=\".($"."_GET['sessionDepth']+1).\"&linkedChild=\".$tableName.\"'>EDIT</a><a class='removeBtn negativeBtns'>REMOVE</a>\";
		$%bodyString .= \"</div></div>\";	
	}	else {
		$%SQLlinkCheck = \"DELETE $%linkTableName.* FROM $%linkTableName LEFT JOIN $%childTableName ON $%childTableName\".\".id = $%linkTableName\".\".child_id WHERE $%childTableName\".\".id IS NULL\";
		$%SQLlinkExecute = $%db->query($%SQLlinkCheck);
	} //end if/else(id)
} //end while ($%rowArray)
?>
	<script type='text/javascript'>
	var jsTableName = '$tableName';
	$(function() {
		jQuery.fileDelete = function(id,field) {
		$.ajax({
			url: \"ajax.php\",
			data: \"tablename=\"+jsTableName+\"&id=\"+id+\"&field=\"+field+\"&AJAXtask=deleteFile\",
			cache: false,
			complete: function(){
				//alert(\"tablename=\"+jsTableName+\"&id=\"+id+\"&field=\"+field+\"&AJAXtask=deleteFile\");
			}
		});
	};	

	jQuery.deleteAllFiles = function(id,field) {
		$fieldsForDeletionString		
		};		
	});
	</script>
	
	<?php echo $%bodyString;";
	$writeFooter = " ?>
	\n
";
	$pageSource = str_replace("%", "", $writePost) . str_replace("%", "", $writeHead) . str_replace("%", "", $fieldVarsString) . str_replace("%", "", $linkCheck) .  $backListElements .  str_replace("%", "", $writeBody) . str_replace("%", "", $writeFooter);
	//echo "<br> <br> backlistelements:" . $backListElements;
	//echo "<br> <br> createList pagesource = " . $pageSource;
	//echo "<h2>tableName = $tableName</h2> ";
	//echo "<br> <br> backlistelements:" . $backListElements;
	//echo "<br> <br> createList pagesource = " . $pageSource;
	//echo "<h2>tableName = $tableName</h2> ";

?>